<?php
//header("content-Type: text/html; charset=Utf-8");//Tieba Client支持Tieba Advanced尾巴版
if(!isset($_GET['type']))exit;
//header("Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
function curl_get($url){
    $ch=curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $content = curl_exec($ch);
    curl_close($ch);
    return $content;
}
function editheader($str){
	$arr = array("// @name        Tieba Client","s://userscripts.org/scripts/source/177032.meta.js","https://userscripts.org/scripts/source/177032.user.js");
	$arr2 = array("// @name        Tieba Client支持Tieba Advanced尾巴版","://hhplus8.duapp.com/download/TCATAT.meta.js","http://hhplus8.duapp.com/download/TCATAT.user.js");
	return str_replace($arr, $arr2, $str, $i);
}
function getmeta(){
	header("Content-Type: text/javascript; charset=utf-8");
	$source=curl_get('http://userscripts.org/scripts/source/177032.meta.js');
	echo editheader($source);
}
function getuser(){
	header("Content-Type: application/x-javascript; charset=UTF-8");
	//header("Content-Disposition: attachment; filename=TCATAT.user.js");
	$source=curl_get('http://userscripts.org/scripts/source/177032.user.js');
	$source=editheader($source);
	$arr = array("function format(content){", "function imgFormat(){","function lzlPost(floor_num,quote_id){","$(this).width()","$(this).height()");
	$arr2 = array("function gettails(floor_num){
    	var lzl='tc={lzl_floor_num:'+(floor_num?floor_num:0)+'};';
		try{
            if(!PageData)var PageData=unsafeWindow.PageData;
			if(!$.parseJSON(localStorage.ge_usetail)) return '';
			var l=localStorage.ge_tailindex,t=$.parseJSON(localStorage.ge_tails);if(!t) return '';
			if(l==0) l=Math.floor(Math.random()*t.length);else if(l>t.length)l=t.length-1; else l--;
			t=t[l];var d=t.data;
			if(t.type=='j') d=eval(lzl+d);
			else if(t.type=='H') {d=d.split('\\n');d=d[Math.floor(Math.random()*d.length)];}
			return '<br><br>'+d;
		}catch(err){return '<br><br>尾巴生'+err+'病了@_@'}
	}
    
	function format(content){", "function imgFormat(){
        	$('#ueditor_replace').append('<p>'+gettails()+'</p>');",'function lzlPost(floor_num,quote_id){
        	$(".lzl_simple_wrapper .tb-editor-editarea").append(gettails(floor_num));',"$(this).width()||1","$(this).height()||1");//\n
	echo str_replace($arr, $arr2, $source, $i);
}
if($_GET['type']=='meta')getmeta();
else if($_GET['type']=='user') getuser();
?>